package com.sunny.ihealth;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.jaeger.library.StatusBarUtil;
import com.sunny.ihealth.data.BmobManager;
import com.sunny.ihealth.data.PreferencesHelper;
import com.sunny.ihealth.data.im.IMMessageHandler;

/**
 * Activity基类，初始化一些功能
 * Created by Sunny  An on 2016/5/14.
 */
public class BaseActivity extends AppCompatActivity {

    private static final String TAG = "TAG::BaseActivity";

    private Toolbar toolbar;

    /**
     * 使返回按钮生效
     */
    private boolean enableBackIcon = false;



    /**
     * 设置Toolbar标题，返回按钮
     *
     * @param title
     */
    public void setToolbar(String title) {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle(title);
            setSupportActionBar(toolbar);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setHomeButtonEnabled(true);
                actionBar.setDisplayHomeAsUpEnabled(true);
            }
        }
    }

    /**
     * 获取toolbar
     *
     * @return
     */
    public Toolbar getToolbar() {
        return toolbar;
    }


    /**
     * 设置状态栏的颜色
     */
    public void setStatusBarColor() {
        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.colorPrimaryDark));
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (enableBackIcon && item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    public boolean isEnableBackIcon() {
        return enableBackIcon;
    }

    /**
     * 设置返回图标
     *
     * @param enableBackIcon
     */
    public void setEnableBackIcon(boolean enableBackIcon) {
        this.enableBackIcon = enableBackIcon;
    }


    /**
     * 重写此方法，初始化ui
     */
    public void initViews() {

    }


    /**
     * 重写此方法，初始化数据
     */
    public void initData() {

    }


    public BaseApplication getBaseApp() {
        return (BaseApplication) getApplication();
    }


    public BmobManager getBmob() {
        return getBaseApp().getBmob();
    }

    public PreferencesHelper getPreferencesHelper() {
        return getBaseApp().getPreferencesHelper();
    }

    public String getCurrentUser() {
        return getBaseApp().getCurrentUser();
    }

    public IMMessageHandler getImMessageHandler() {
        return getBaseApp().getImMessageHandler();
    }


}
